from tjob.initialize.setup_envs import _CACHE, clear_envs
from .base_command import BaseCommand
from cleo.helpers import option
from tjob.meta import JobManger
import subprocess


class ClearCommand(BaseCommand):
    name = "clear"
    description = "Clear ALL data, like cache, jobs."
    options = [
        option(
            "jobs",
            "j",
            flag=True,
            description="clear all recording jobs",
        ),
        option(
            "cache",
            "c",
            flag=True,
            description="clear cache directory in tjob"
            + "[NOTE]: it including all config and mid results",
        ),
    ]

    def handle(self):
        if self.option("jobs"):
            JobManger().clear()
            self.blue_text("Clear all Jobs in JobManger")

        elif self.option("cache"):
            clear_envs(_CACHE)
            self.red_text("Clear... Please re-init if you want use tjob")
        else:
            subprocess.call(["tjob", "clear", "-h"])
            self.green_text(
                "\n\nPlease give clean options. E.g. clear cache: tjob clear cache"
            )
